package com.bigflexlib.x.mx.collections.IList
{
	import flash.events.TimerEvent;
	import flash.utils.Dictionary;
	import flash.utils.Timer;
	
	import mx.collections.IList;
	
	public class SlowFiller
	{
		private var timer:Timer;
		
		private var values:Array;
		
		private var i:int=0;
		
		private var list:IList;
		
		public function SlowFiller( list:IList, values:Array, delay:int=100 )
		{
			this.values = values;
			this.list = list;
			this.list.removeAll();
			timer = new Timer(delay);
			timer.addEventListener( TimerEvent.TIMER, timer_timer );
			timer.start();
		}

		private function timer_timer( event:TimerEvent ):void
		{
			if ( i == values.length )
				dispose();								
			else
				list.addItem( values[i++] );
		}
		
		private function dispose():void
		{
			timer.stop();
			list = null;
			delete cache[this];
		}
		
		
		private static var cache:Dictionary = new Dictionary(true);
		public static function fill( list:IList, values:Array=null, delay:int=100 ):SlowFiller
		{
			if ( cache[list] )
				cache[list].dispose();
			var filler:SlowFiller= new SlowFiller( list, values, delay );
			cache[list] = filler;
			return filler;
		}

	}
}